package com.ljx.itbbs.server.article.service.impl;

import com.ljx.itbbs.common.constant.CacheConstants;
import com.ljx.itbbs.common.service.algorithm.TrieUtil;
import com.ljx.itbbs.common.utils.AssertUtil;
import com.ljx.itbbs.server.article.dao.SensitiveDao;
import com.ljx.itbbs.server.article.domain.entity.SensitiveEntity;
import com.ljx.itbbs.server.article.service.SensitiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-04-20  23:50
 */
@Service
public class SensitiveServiceImpl implements SensitiveService {

    @Autowired
    private SensitiveDao sensitiveDao;

    @PostConstruct
    public void init(){
        this.getAll().stream().forEach(item -> TrieUtil.insert(item.getWord()));
//        String key = CacheConstants.getKey(CacheConstants.SENSITIVE_WORDS);
//        // 先清空铭感词列表
//        stringRedisTemplate.delete(key);
//        // 再添加
//        sensitiveAll.stream().forEach(
//                item -> stringRedisTemplate.opsForSet().add(key, item)
//        );
    }

    @Override
    public void checkSensitive(String content) {
        boolean isFind = TrieUtil.find(content);
        AssertUtil.isFalse(isFind, "评论存在敏感词汇");
    }

    @Override
    public List<SensitiveEntity> getAll() {
        return sensitiveDao.list();
    }
}
